package com.suxii.netty_demo.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.suxii.adapter.NewFriendAdapter;
import com.suxii.netty_demo.R;
import com.suxii.netty_demo.fragment.FriendFragment;
import com.suxii.utils.Global;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class NewFriendActivity extends AppCompatActivity {

    private RecyclerView newFriendList;
    private TextView resText;

    private Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_friend);
        context=this;

        newFriendList=findViewById(R.id.newfriend_list);
        resText=findViewById(R.id.newfriend_res);

        init();

    }
    private void init(){
        resText.setVisibility(Global.newFriendList.size()>0?View.GONE:View.VISIBLE);

        LinearLayoutManager manager = new LinearLayoutManager(this);
        NewFriendAdapter newFriendAdapter = new NewFriendAdapter(Global.newFriendList, this);
        newFriendList.setLayoutManager(manager);
        newFriendList.setAdapter(newFriendAdapter);

        newFriendAdapter.OnNewFriendListener(new NewFriendAdapter.NewFriendListener() {
            @Override
            public void onAgree(String uuid, TextView agree) {
                if (agree.getText().toString().equals("添加")) {
                    new Thread(() -> agreeFriend(uuid)).start();
                    agree.post(()-> agree.setText("已添加"));
                }
            }

            @Override
            public void onDisagree(String uuid) {
                removeNewFriend(uuid);
                newFriendAdapter.notifyDataSetChanged();
                resText.setVisibility(Global.newFriendList.size()>0?View.GONE:View.VISIBLE);
                smallMsg();
                // 新朋友消息提醒
                Suxii.circleOpenView.receiveMsg(Global.newFriendList.size()>0);
            }
        });
    }

    private void smallMsg() {
        // 更新新朋友小消息数
        if (FriendFragment.friendCircleText!=null){
            FriendFragment.friendCircleText.post(() -> {
                FriendFragment.friendCircleText.setText(Global.newFriendList.size()+"");
            });
        }
        Suxii.circleOpenView.receiveMsg(Global.newFriendList.size()>0);
    }

    private void agreeFriend(String uuid){
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                .url(getString(R.string.url) + "add-friend?uid=" + Global.uid + "&uuid=" + uuid)
                .get()
                .build();
        Call call = client.newCall(request);
        try {
            Response execute = call.execute();
            String res = execute.body().string();
            JsonObject jsonObject = new Gson().fromJson(res, JsonObject.class);
            if (String.valueOf(jsonObject.get("code")).equals("1")) {
                Log.e("aa","已添加");
                removeNewFriend(uuid);
            }else {
                Log.e("aa","操作失败");
            }


        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private void removeNewFriend(String uuid) {
        for (int i = 0; i < Global.newFriendList.size(); i++) {
            if (Global.newFriendList.get(i).equals(uuid)) {
                Global.newFriendList.remove(i);
                return;
            }
        }
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            smallMsg();
            finish();
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }
}